<template>
  <basic-container style="padding-bottom: 50px">
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :permission="permissionList"
               :before-open="beforeOpen"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad"
               v-if="showMain"
    >
      <template slot-scope="{type,size,row,index}" slot="menuBtn">
        <el-dropdown-item divided @click.native="showView(row)">查看用户组</el-dropdown-item>
      </template>
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   v-if="permission.role_delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
    <userGroupList v-bind:csId="csId" :showMainView="showMainView" v-if="(!showMain)&&show" ref="eps1"></userGroupList>
  </basic-container>
</template>

<script>
import {add, getList, remove, update} from "@/api/forum/userGroup";
import userGroupList from "@/views/forum/userGroupList.vue";
import {mapGetters} from "vuex";
import {getDetail} from "@/api/service/bj";

export default {
  components: {
    userGroupList
  },
  data() {
    return {
      showMain: true,
      show: false,
      csId: '',
      form: {},
      box: false,
      props: {
        label: "title",
        value: "key"
      },
      menuGrantList: [],
      dataScopeGrantList: [],
      menuTreeObj: [],
      dataScopeTreeObj: [],
      selectionList: [],
      query: {type: '0'},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        tree: true,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        menuType: 'menu',
        menuBtnTitle: '更多操作',
        column: [
          // {
          //   label: "学校名称",
          //   prop: "schoolId",
          //   type: "tree",
          //   search: true,
          //   span: 24,
          //   rules: [{
          //     required: true,
          //     message: "请选择学校名称",
          //     trigger: "blur"
          //   }],
          //   // hide:true,parentId
          //   cascader: ['parentId'],
          //   dicUrl: "/api/PcPublicService/GetSchoolTreeSelect",
          //   props: {
          //     label: "title",
          //     value: "id"
          //   },
          // },
          {
            label: "上级分组",
            prop: "parentName",
            editDisplay: false,
            viewDisplay: true,
            addDisplay: false,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入上级分组",
                trigger: "blur"
              }
            ]
          },
          {
            label: "上级分组",
            prop: "parentId",
            type: "tree",
            hide: true,
            editDisplay: true,
            viewDisplay: false,
            addDisplay: true,
            span: 24,
            dicUrl: "/api/ManageUserGroupInit/GetUserGroupSelect",
            rules: [
              {
                required: false,
                message: "请选择上级分组",
                trigger: "click"
              }
            ],
            props: {
              label: "groupName",
              value: "id"
            },

          },
          {
            label: "分组名称",
            prop: "groupName",
            search: true,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入分组名称",
                trigger: "blur"
              }
            ]
          },
          {
            label: "分组描述",
            prop: "groupDes",
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入分组名称",
                trigger: "blur"
              }
            ]
          },
          {
            label: "排序",
            prop: "sort",
            type: "number",
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入排序",
                trigger: "blur"
              }
            ]
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.role_add, false),
        viewBtn: this.vaildData(this.permission.role_view, false),
        delBtn: this.vaildData(this.permission.role_delete, false),
        editBtn: this.vaildData(this.permission.role_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
    idsArray() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids;
    }
  },
  methods: {
    rowSave(row, done, loading) {
      row.parentId = row.parentId === '' ? '0' : row.parentId
      console.log(row)
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      row.parentId = row.parentId === '' ? '0' : row.parentId
      console.log(row)

      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      if (row.groupType === 1) {
        this.$message({
          type: "warning",
          message: "系统级用户组不可操作!"
        });
        return 0
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    beforeOpen(done, type) {
      if (["edit"].includes(type)) {
        if (this.form.groupType === 1) {
          this.$message({
            type: "warning",
            message: "系统级用户组不可操作!"
          });
          return 0
        } else {
          done();
        }
      } else {
        done();
      }

    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      params.parentId = '0'
      params.type = 1
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },

    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      console.log(Object.assign(params, this.query), params, this.query)
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        this.data = res.data.data;
        this.loading = false;
      });
    },
    showView(row) {
      if (row.groupType === 1) {
        this.$message({
          type: "warning",
          message: "系统级用户组不可操作!"
        });
        return 0
      }
      this.csId = row.id;
      //this.$refs.eps.onLoad({currentPage:1,pageSize:10});
      this.showMain = false;
      this.show = true;
    },
    showMainView() {
      this.showMain = true;
      this.show = false;
    },
  }
};
</script>

<style>
</style>
